package com.l.im;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @program: LIM
 * @description:
 * @author: Alex Wu
 * @createDate: 2025-04-15 15:44
 **/
public class RocketMQConsumerDemo {
    public static void main(String[] args) throws Exception{
        //创建消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group-java");
        //设置 broker 地址
        consumer.setNamesrvAddr("127.0.0.1:9876");
        consumer.subscribe("single-im-a", "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    log.info("接受消息：队列 ID==>"+messageExt.getQueueId() + " 消息 Id:" + messageExt.getMsgId());
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            } });
        consumer.start();
        //System.in.read();
    }
}
